import os

import requests
from celery.utils.log import get_task_logger
from config.const import Const

from . import app

DING_TALK_ALARM_URL = os.getenv('DING_TALK_ALARM_URL')

logger = get_task_logger(__name__)


@app.task
def ding_alarm(msg):
    """发送到钉钉报警群"""
    if Const.ENV in ('test', 'prod'):
        r = requests.post(DING_TALK_ALARM_URL, json={'msgtype': 'text', 'text': {'content': msg}})
        logger.info(f'status_code: {r.status_code}, content: {r.content}')
